import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Demo09 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        A a = new A();
        frame.setSize(500,500);
        frame.setTitle("安梓嘉专属小球");
        Thread thread = new Thread(a);
        thread.start();
        frame.addWindowListener(a);
        frame.add(a);
        frame.setBackground(Color.CYAN);
        frame.setVisible(true);
    }


}

class  A extends Panel implements Runnable, WindowListener {
    int x = 20;
    int y = 20;
    @Override
    public void paint(Graphics g) {
       g.fillOval(x,y,20,20);
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    @Override
    public void run() {
            while (true){
                y++;
                if (y>500){
                    y=0;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                repaint();
            }
    }
}